using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class FormCreateNewVerb : Forms.FormCreateNewBase
	{
		#region Initialization
		public FormCreateNewVerb(Repository.Verb verb, bool allowSaveContinue)
			: base(verb, verb, allowSaveContinue, OpenComposite.EII.Repository.ItemType.Composite)
		{
			InitializeComponent();

			displayInputOutput();
		}
		#endregion

		#region Protected Members

		#region Methods

		protected override void OnValidateNewItem(CancelEventArgs e)
		{
			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name for the capability", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				txtName.Focus();
				e.Cancel = true;
				return;
			}
			DataTable dt = Global.Data.SearchRepositoryItems(
				Global.Status.CurrentDomainId, this.RepositoryItem.RepositoryItemType,
				txtName.Text, null, true);
			if ( dt != null && dt.Rows.Count > 0 ) {
				MessageBox.Show("An capability with this name exists in the current domain." + Environment.NewLine + Environment.NewLine +
								"Please enter another name.",
								this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
				txtName.Focus();
				txtName.SelectAll();
				e.Cancel = true;
				return;
			}
			base.OnValidateNewItem(e);
		}
		protected override void OnSaveClose(CancelEventArgs e)
		{
			if ( save() ) {
				base.OnSaveClose(e);
			} else {
				e.Cancel = true;
			}
		}
		protected override void OnSaveContinue(CancelEventArgs e)
		{
			if ( save() ) {
				base.OnSaveContinue(e);
			} else {
				e.Cancel = true;
			}
		}
		protected override void OnCancel()
		{
			base.OnCancel();
		}
		#endregion

		#endregion

		#region Private Members

		#region Fields
		private Repository.BusinessObject _inputBObject = null;
		private Repository.BusinessObject _outputBObject = null;
		#endregion

		#region Methods

		private bool save()
		{
			Repository.Verb verb = this.RepositoryItem as Repository.Verb;
			if ( verb == null )
				throw new Exception("Repository Item is not a capability.");

			verb.Name = txtName.Text;
			verb.Description = txtDesc.Text;
			verb.InputBusinessObject = _inputBObject;
			verb.OutputBusinessObject = _outputBObject;

			verb.DomainId = this.DomainID;

			return true;
		}

		private void displayInputOutput()
		{
			this.SuspendLayout();
			txtInput.SuspendLayout();
			txtOutput.SuspendLayout();
			if ( _inputBObject == null ) {
				txtInput.ForeColor = SystemColors.GrayText;
				txtInput.TextAlign = HorizontalAlignment.Center;
				txtInput.Text = "Please select an input.";
				btnRemoveInput.Enabled = false;
				btnViewInput.Enabled = false;
			} else {
				txtInput.ForeColor = SystemColors.ControlText;
				txtInput.TextAlign = HorizontalAlignment.Left;
				txtInput.Text = _inputBObject.Name;
				btnRemoveInput.Enabled = true;
				btnViewInput.Enabled = true;
			}
			if ( _outputBObject == null ) {
				txtOutput.ForeColor = SystemColors.GrayText;
				txtOutput.TextAlign = HorizontalAlignment.Center;
				txtOutput.Text = "Please select an output.";
				btnRemoveOutput.Enabled = false;
				btnViewOutput.Enabled = false;
			} else {
				txtOutput.ForeColor = SystemColors.ControlText;
				txtOutput.TextAlign = HorizontalAlignment.Left;
				txtOutput.Text = _outputBObject.Name;
				btnRemoveOutput.Enabled = true;
				btnViewOutput.Enabled = true;
			}
			txtInput.ResumeLayout(false);
			txtOutput.ResumeLayout(false);
			this.ResumeLayout(true);
		}
		private void viewSchema(Repository.BusinessObject bo)
		{
			if ( bo == null || bo.Schema == null )
				return;

			XmlDocument xd = new XmlDocument();
			using ( StringWriter sw = new StringWriter() ) {
				bo.Schema.Write(sw);
				xd.LoadXml(sw.ToString());
			}
			formXmlViewer XmlViewer = new formXmlViewer();
			XmlViewer.XmlDocument = xd;
			XmlViewer.Text = "XML-Schema of Business Object \"" + bo.Name + "\"";
			XmlViewer.ShowDialog();
		}
		private void selectBusinessObject(bool isInput)
		{
			//Repository.BusinessObject resultBO = null;
			Forms.FormSelectRepositoryEx frm = new OpenComposite.EII.Forms.FormSelectRepositoryEx(this.RepositoryItem);
			frm.AllowedTypes = new Repository.ItemType[] { Repository.ItemType.BObject };
			frm.DomainID = Global.Status.CurrentDomainId;
			frm.RepositoryID = Global.Status.CurrentRepositoryId;
			frm.MultiSelect = false;
			frm.SelectedItem = isInput ? _inputBObject : _outputBObject;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				if ( isInput ) {
					_inputBObject = frm.SelectedItem as Repository.BusinessObject;
				} else {
					_outputBObject = frm.SelectedItem as Repository.BusinessObject;
				}
				displayInputOutput();
			}
		}
		#endregion

		#region Event Handlers

		private void btnSelInput_Click(object sender, EventArgs e)
		{
			selectBusinessObject(true);
		}
		private void btnViewInput_Click(object sender, EventArgs e)
		{
			viewSchema(_inputBObject);
		}
		private void btnRemoveInput_Click(object sender, EventArgs e)
		{
			_inputBObject = null;
			displayInputOutput();
		}

		private void btnSelOutput_Click(object sender, EventArgs e)
		{
			selectBusinessObject(false);
		}
		private void btnViewOutput_Click(object sender, EventArgs e)
		{
			viewSchema(_outputBObject);
		}
		private void btnRemoveOutput_Click(object sender, EventArgs e)
		{
			_outputBObject = null;
			displayInputOutput();
		}

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		private void txtDesc_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if ( e.Control && e.KeyCode == Keys.Enter ) {
				CancelEventArgs cargs = new CancelEventArgs(false);
				OnValidateNewItem(cargs);
				if ( !cargs.Cancel ) {
					if ( this.AllowContinue )
						OnSaveContinue(new CancelEventArgs(false));
					else
						OnSaveClose(new CancelEventArgs(false));
				}
			}
		}

		#endregion

		#endregion
	}
}